﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mhxp
{
	public partial class MainForm : Form
	{
		private Timer _tmr = new Timer();
		private bool _play = false;
		private int _pindex = 0;
		private string _lastpath = "";
		private Point _pixsize;

		public MainForm()
		{
			InitializeComponent();

			//
			cboFramePerSecond.SelectedIndex = 30 - 1;
			chkAutoSort.Checked = true;

			//
			_tmr.Tick += TickPlay;
		}

		private void MainForm_Load(object sender, EventArgs e)
		{
			CreateNew();
		}

		private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
		{

		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{

		}

		private void lstPixs_DragOver(object sender, DragEventArgs e)
		{
			if (_play)
			{
				e.Effect = DragDropEffects.None;
			}
			else if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				e.Effect = DragDropEffects.Copy;
			}
		}

		private void lstPixs_DragDrop(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				//_lastpath = "";

				string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

				ReadFileForm frm = new ReadFileForm(files, _lastpath, _pixsize);
				frm.ShowDialog();

				List<ListViewItem> items = frm.Items;
				if (items.Count>0)
				{
					lstPixs.BeginUpdate();

					foreach (ListViewItem li in items)
						lstPixs.Items.Add(li);

					lstPixs.EndUpdate();

					if (chkAutoSort.Checked)
						SortList();

					_lastpath = frm.LastPath;
					_pixsize = frm.PixSize;
				}

				CheckUis();
			}
		}

		private void lstPixs_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (lstPixs.SelectedItems.Count == 0)
				return;

			ListViewItem li = lstPixs.SelectedItems[0];

			Bitmap bmp = (Bitmap)li.Tag;
			string s = (string)bmp.Tag;

			lblFileName.Text = s;
			pxView.Image = bmp;

			//
			btnListUp.Enabled = lstPixs.SelectedIndices[0] == 0 ? false : true;
			btnListDown.Enabled = lstPixs.SelectedIndices[lstPixs.SelectedIndices.Count - 1] == lstPixs.Items.Count - 1 ? false : true;
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			CreateNew();
		}

		private void CreateNew()
		{
			if (_play)
				StopPlay();

			_pindex = 0;
			//_lastpath = "";
			_pixsize = new Point(0, 0);

			lstPixs.Items.Clear();
			lblFileName.Text = "";
			pxView.Image = null;

			CheckUis();
		}

		private void btnListUp_Click(object sender, EventArgs e)
		{
			// 구현 안됨
		}

		private void btnListDown_Click(object sender, EventArgs e)
		{
			// 구현 안됨
		}

		private void CheckUis()
		{
			bool e = false;

			if (lstPixs.Items.Count != 0)
				e = true;

			btnListUp.Enabled = e;
			btnListDown.Enabled = e;
		}

		private void btnPlay_Click(object sender, EventArgs e)
		{
			if (_play)
				StopPlay();
			else
				StartPlay();
		}

		private void StartPlay()
		{
			if (lstPixs.Items.Count == 0)
				return;

			_pindex = lstPixs.SelectedIndices.Count > 0 ? lstPixs.SelectedIndices[0] : 0;
			_play = true;

			btnPlay.Text = "정지";

			string sel = (string)cboFramePerSecond.SelectedItem;

			_tmr.Interval = 1000 / Convert.ToInt32(sel);
			_tmr.Start();
		}

		private void StopPlay()
		{
			_play = false;

			_tmr.Stop();

			btnPlay.Text = "재생";
		}

		private void TickPlay(object sender, EventArgs e)
		{
			int max = lstPixs.Items.Count;

			if (_pindex < max)
			{
				ListViewItem li = lstPixs.Items[_pindex];
				Bitmap bmp = (Bitmap)li.Tag;
				string s = (string)bmp.Tag;

				lblFileName.Text = s;
				pxView.Image = bmp;
			}

			//
			_pindex++;

			if (_pindex >= max)
			{
				if (chkLoop.Checked)
					_pindex = 0;
				else
				{
					// 정지
					StopPlay();
				}
			}
		}

		private void ctiPixsDelete_Click(object sender, EventArgs e)
		{

		}

		private void ctiPixsClear_Click(object sender, EventArgs e)
		{
			if (_play)
				return;

			lstPixs.Items.Clear();

			lblFileName.Text = "";
			pxView.Image = null;
		}

		private void SortList()
		{
			if (_play)
				return;

			int max = lstPixs.Items.Count;

			ListViewItem[] lis = new ListViewItem[max];

			for (int i = 0; i < max; i++)
				lis[i] = lstPixs.Items[i];

			if (SBCS.Test)
			{
				Array.Sort(lis, delegate(ListViewItem i1, ListViewItem i2)
				{
					string s1 = i1.Text;
					string s2 = i2.Text;
					return SBCS.k_strnatcasecmp(s1, s2);
				});
			}
			else
			{
				Array.Sort(lis, delegate(ListViewItem i1, ListViewItem i2)
				{
					string s1 = i1.Text;
					string s2 = i2.Text;
					return s1.CompareTo(s2);
				});
			}

			lstPixs.SelectedItems.Clear();

			lstPixs.BeginUpdate();
			lstPixs.Items.Clear();

			foreach (ListViewItem li in lis)
				lstPixs.Items.Add(li);

			lstPixs.EndUpdate();
		}

		private void ctiPixsSort_Click(object sender, EventArgs e)
		{
			SortList();
		}

		private void cboFramePerSecond_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (_play)
				StopPlay();
		}

		private void btnOpen_Click(object sender, EventArgs e)
		{

		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (_play)
				StopPlay();

			if (!SBCS.Test)
			{
				MessageBox.Show("저장할 수 없습니다. 필요한 기능을 가진 DLL을 찾을 수 없습니다.");
				return;
			}

			int max = lstPixs.Items.Count;

			if (max == 0)
				return;

			SaveFileDialog dlg = new SaveFileDialog();
			dlg.InitialDirectory = _lastpath;
			dlg.Filter = "hxp 파일 (*.hxp)|*.hxp|모든 파일 (*.*)|*.*";
			dlg.FilterIndex = 1;
			dlg.RestoreDirectory = true;
			dlg.OverwritePrompt = true;
			dlg.AddExtension = true;

			if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				List<Bitmap> bmps = new List<Bitmap>(max);
				foreach (ListViewItem li in lstPixs.Items)
					bmps.Add((Bitmap)li.Tag);

				SaveHxpForm d = new SaveHxpForm(dlg.FileName, bmps, Convert.ToInt32((string)cboFramePerSecond.SelectedItem), _pixsize);
				d.ShowDialog();

				FileInfo fi = new FileInfo(dlg.FileName);
				_lastpath = fi.DirectoryName;

				if (chkNewFileAfterSave.Checked)
					CreateNew();
			}
		}
	}
}
